<template>
  <div class="home-container">
    <!-- 顶部导航栏 -->
    <div class="header-nav">
      <div class="nav-brand">
        <h1>SKT StageWardrobe</h1>
      </div>
      <div class="nav-actions">
        <template v-if="userStore.isLoggedIn">
          <span class="welcome-text">欢迎回来，{{ userStore.getUser.username || '用户' }}！</span>
          <el-button type="primary" @click="$router.push('/user/info')">个人中心</el-button>
          <el-button @click="handleLogout">退出登录</el-button>
        </template>
        <template v-else>
          <el-button type="primary" @click="$router.push('/login')">登录</el-button>
          <el-button @click="$router.push('/register')">注册</el-button>
        </template>
      </div>
    </div>

    <main class="main-content">
      <!-- 欢迎区域 -->
      <div class="welcome-section">
        <h2>欢迎使用 SKT StageWardrobe</h2>
        <p class="subtitle">专业的舞台服装管理系统</p>
      </div>

      <!-- 用户功能区域 -->
      <div class="user-dashboard">
        <el-row :gutter="20">
          <!-- 用户中心卡片（仅登录后显示） -->
          <el-col :xs="24" :sm="12" :lg="8" v-if="userStore.isLoggedIn">
            <el-card class="dashboard-card user-center-card">
              <template #header>
                <div class="card-header">
                  <span>用户中心</span>
                  <el-icon><User /></el-icon>
                </div>
              </template>
              <div class="card-content">
                <p>欢迎回来，<strong>{{ userStore.getUser.username || '用户' }}</strong>！</p>
                <div class="action-buttons">
                  <el-button type="primary" @click="$router.push('/user/info')" round>
                    个人信息管理
                  </el-button>
                  <el-button @click="handleNavigate('/item')" round>
                    物品管理
                  </el-button>
                </div>
              </div>
            </el-card>
          </el-col>

          <!-- 快速入口卡片（始终显示） -->
          <el-col :xs="24" :sm="12" :lg="8">
            <el-card class="dashboard-card quick-access-card">
              <template #header>
                <div class="card-header">
                  <span>快速入口</span>
                  <el-icon><Operation /></el-icon>
                </div>
              </template>
              <div class="card-content">
                <el-space direction="vertical" style="width: 100%">
                  <el-button type="primary" @click="handleNavigate('/costumes')" style="width: 100%">
                    👗 浏览服装
                  </el-button>
                  <el-button @click="handleNavigate('/item')" style="width: 100%">
                    📦 物品管理
                  </el-button>
                  <el-button @click="handleNavigate('/item-management')" style="width: 100%">
                    📋 库存管理
                  </el-button>
                  <el-button @click="handleNavigate('/add-item')" style="width: 100%">
                    ➕ 添加物品
                  </el-button>
                </el-space>
              </div>
            </el-card>
          </el-col>

          <!-- 登录提示卡片（未登录时显示） -->
          <el-col :xs="24" :sm="12" :lg="8" v-if="!userStore.isLoggedIn">
            <el-card class="dashboard-card login-prompt-card">
              <template #header>
                <div class="card-header">
                  <span>用户登录</span>
                  <el-icon><UserFilled /></el-icon>
                </div>
              </template>
              <div class="card-content">
                <p>请登录以使用完整功能</p>
                <div class="action-buttons">
                  <el-button type="primary" @click="$router.push('/login')" round>
                    立即登录
                  </el-button>
                  <el-button link @click="$router.push('/register')" round>
                    注册账号
                  </el-button>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>

      <!-- 服装展示区域 -->
      <div class="costume-showcase">
        <div class="section-header">
          <h3>精选服装展示</h3>
          <p>发现最适合您的舞台服装</p>
          <el-button type="primary" @click="$router.push('/costumes')" class="view-all-btn">
            查看全部服装 <el-icon><ArrowRight /></el-icon>
          </el-button>
        </div>

        <!-- 修复条件渲染结构 -->
        <div v-if="loading" class="loading-container">
          <el-skeleton :rows="4" animated />
        </div>

        <div v-else-if="featuredCostumes.length === 0" class="empty-container">
          <el-empty description="暂无服装数据">
            <el-button type="primary" @click="$router.push('/add-item')">
              添加服装
            </el-button>
          </el-empty>
        </div>

        <div v-else class="costume-grid">
          <el-row :gutter="20">
            <el-col
                v-for="costume in featuredCostumes"
                :key="costume.id"
                :xs="12"
                :sm="8"
                :md="6"
            >
              <el-card
                  class="costume-card"
                  shadow="hover"
                  :body-style="{ padding: '0' }"
                  @click="viewCostumeDetail(costume.id)"
              >
                <div class="image-container">
                  <el-image
                      :src="costume.imageUrl"
                      :alt="costume.name"
                      fit="cover"
                      class="costume-image"
                      :preview-src-list="[costume.imageUrl]"
                  >
                    <template #error>
                      <div class="image-error">
                        <el-icon><Picture /></el-icon>
                        <span>图片加载失败</span>
                      </div>
                    </template>
                    <template #placeholder>
                      <div class="image-loading">
                        <el-icon><Loading /></el-icon>
                      </div>
                    </template>
                  </el-image>
                  <div class="costume-status">
                    <el-tag
                        :type="costume.status === 1 ? 'success' : 'danger'"
                        size="small"
                    >
                      {{ costume.status === 1 ? '可租' : '已租出' }}
                    </el-tag>
                  </div>
                </div>

                <div class="costume-info">
                  <h4 class="costume-name" :title="costume.name">
                    {{ costume.name }}
                  </h4>
                  <p class="costume-meta">
                    <span class="category">{{ costume.category }}</span>
                    <span class="brand">{{ costume.brand }}</span>
                  </p>
                  <div class="costume-price">
                    <span class="price">¥{{ costume.rentalPrice }}</span>
                    <span class="price-unit">/天</span>
                  </div>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </div>
      </div>

      <!-- 功能特色介绍 -->
      <div class="features-section">
        <el-row :gutter="30">
          <el-col :xs="24" :sm="8">
            <div class="feature-card">
              <el-icon size="48" color="#409EFF"><Search /></el-icon>
              <h4>智能搜索</h4>
              <p>快速找到您需要的服装，支持分类、品牌、尺寸等多维度筛选</p>
            </div>
          </el-col>
          <el-col :xs="24" :sm="8">
            <div class="feature-card">
              <el-icon size="48" color="#67C23A"><Monitor /></el-icon>
              <h4>在线管理</h4>
              <p>随时随地管理您的服装库存，实时更新服装状态和租赁信息</p>
            </div>
          </el-col>
          <el-col :xs="24" :sm="8">
            <div class="feature-card">
              <el-icon size="48" color="#E6A23C"><DataAnalysis /></el-icon>
              <h4>数据统计</h4>
              <p>详细的数据分析报告，帮助您更好地了解服装使用情况</p>
            </div>
          </el-col>
        </el-row>
      </div>
    </main>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/pinia'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  User,
  Operation,
  UserFilled,
  ArrowRight,
  Picture,
  Loading,
  Search,
  Monitor,
  DataAnalysis
} from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

// 响应式数据
const featuredCostumes = ref([])
const loading = ref(false)

// 生命周期
onMounted(() => {
  loadFeaturedCostumes()
})

// 方法
const loadFeaturedCostumes = async () => {
  loading.value = true
  try {
    // 模拟获取精选服装数据，实际应该调用API
    featuredCostumes.value = [
      {
        id: 1,
        name: '古典汉服',
        category: '古装',
        brand: '汉尚华莲',
        rentalPrice: 199,
        status: 1,
        imageUrl: 'https://via.placeholder.com/300x400?text=古典汉服'
      },
      {
        id: 2,
        name: '现代礼服',
        category: '现代装',
        brand: '定制品牌',
        rentalPrice: 299,
        status: 1,
        imageUrl: 'https://via.placeholder.com/300x400?text=现代礼服'
      },
      {
        id: 3,
        name: '民族服饰',
        category: '民族服装',
        brand: '传统工艺',
        rentalPrice: 159,
        status: 1,
        imageUrl: 'https://via.placeholder.com/300x400?text=民族服饰'
      },
      {
        id: 4,
        name: '舞台戏服',
        category: '戏服',
        brand: '专业定制',
        rentalPrice: 399,
        status: 0,
        imageUrl: 'https://via.placeholder.com/300x400?text=舞台戏服'
      }
    ]
  } catch (error) {
    console.error('加载精选服装失败:', error)
    ElMessage.error('加载服装数据失败')
  } finally {
    loading.value = false
  }
}

const handleNavigate = (path) => {
  if (path !== '/costumes' && !userStore.isLoggedIn) {
    ElMessage.warning('请先登录以使用此功能')
    router.push('/login')
    return
  }
  router.push(path)
}

const viewCostumeDetail = (id) => {
  router.push(`/costumes/${id}`)
}

const handleLogout = async () => {
  try {
    await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      type: 'warning'
    })

    userStore.clearUser()
    ElMessage.success('退出成功')
    router.push('/')
  } catch (error) {
    if (error === 'cancel') return
    ElMessage.error('退出失败')
  }
}
</script>

<style scoped>
.home-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4efe9 100%);
}

/* 顶部导航栏样式 */
.header-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 40px;
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.nav-brand h1 {
  margin: 0;
  color: #409eff;
  font-size: 24px;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 15px;
}

.welcome-text {
  color: #666;
  font-size: 14px;
}

.main-content {
  padding: 40px 20px;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

.welcome-section {
  text-align: center;
  margin-bottom: 60px;
}

.welcome-section h2 {
  font-size: 2.5rem;
  color: #333;
  margin-bottom: 10px;
}

.subtitle {
  font-size: 1.2rem;
  color: #666;
}

.user-dashboard {
  margin-bottom: 60px;
}

.dashboard-card {
  min-height: 200px;
  margin-bottom: 20px;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  border: none;
  border-radius: 12px;
}

.dashboard-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: bold;
  font-size: 18px;
  color: #333;
}

.card-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
  padding: 20px 0;
}

.action-buttons {
  margin-top: 20px;
  display: flex;
  gap: 10px;
  justify-content: center;
}

/* 不同卡片的特色样式 */
.user-center-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.user-center-card :deep(.el-card__header) {
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  color: white;
}

.quick-access-card {
  background: white;
}

.login-prompt-card {
  background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
}

.login-prompt-card :deep(.el-card__header) {
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}

/* 服装展示区域样式 */
.costume-showcase {
  margin-bottom: 80px;
}

.section-header {
  text-align: center;
  margin-bottom: 40px;
}

.section-header h3 {
  font-size: 2rem;
  color: #333;
  margin-bottom: 10px;
}

.section-header p {
  font-size: 1.1rem;
  color: #666;
  margin-bottom: 20px;
}

.view-all-btn {
  margin-top: 10px;
}

.loading-container {
  padding: 40px 0;
  text-align: center;
}

.empty-container {
  padding: 60px 0;
  text-align: center;
}

.costume-grid {
  margin-bottom: 30px;
}

.costume-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: none;
  border-radius: 8px;
  overflow: hidden;
}

.costume-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.image-container {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.costume-image {
  width: 100%;
  height: 100%;
  transition: transform 0.3s ease;
}

.costume-card:hover .costume-image {
  transform: scale(1.05);
}

.image-error,
.image-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #999;
  background: #f5f7fa;
}

.costume-status {
  position: absolute;
  top: 8px;
  right: 8px;
}

.costume-info {
  padding: 15px;
}

.costume-name {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 8px;
  line-height: 1.4;
  height: 44px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.costume-meta {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 12px;
  color: #909399;
}

.costume-price {
  text-align: right;
}

.price {
  font-size: 20px;
  font-weight: bold;
  color: #e6a23c;
}

.price-unit {
  font-size: 12px;
  color: #909399;
  margin-left: 2px;
}

/* 功能特色区域 */
.features-section {
  margin-top: 60px;
}

.feature-card {
  text-align: center;
  padding: 30px 20px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s ease;
}

.feature-card:hover {
  transform: translateY(-5px);
}

.feature-card h4 {
  margin: 20px 0 10px;
  color: #333;
  font-size: 18px;
}

.feature-card p {
  color: #666;
  line-height: 1.6;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-nav {
    padding: 15px 20px;
    flex-direction: column;
    gap: 15px;
  }

  .main-content {
    padding: 20px 10px;
  }

  .welcome-section h2 {
    font-size: 2rem;
  }

  .action-buttons {
    flex-direction: column;
  }

  .nav-actions {
    flex-wrap: wrap;
    justify-content: center;
  }

  .section-header h3 {
    font-size: 1.5rem;
  }

  .image-container {
    height: 150px;
  }
}
</style>